import React, { Component } from 'react';
import BaseComponent from '@/components/BaseComponent';
import styles from './index.less';
import { connect } from 'dva';
import SearchForm from './SearchForm';
import StandardTable from '@/components/StandardTable';
import { Card } from 'antd';

/**
 * 网报进度
 */
@connect(({ user, reportProgress, loading }) => ({
  currentUser: user.currentUser,
  data: reportProgress.netReportData,
  isLoading: loading.effects['reportProgress/getPageData'],
  isDownloading: loading.effects['reportProgress/queryAllExportData'],
}))
class NetReportProgress extends BaseComponent {
  columns = [
    {
      title: '城市',
      key: 'city_name',
      dataIndex: 'city_name',
      width: 80,
    },
    {
      title: '区县',
      key: 'region_name',
      dataIndex: 'region_name',
      width: 100,
    },
    {
      title: '上报机构',
      key: 'institution_name',
      dataIndex: 'institution_name',
      width: 100,
    },
    {
      title: '网报总数',
      key: 'total',
      dataIndex: 'total',
      width: 100,
    },
    // 网报总数
    {
      title: '网报总数',
      key: 'wbzs',
      dataIndex: 'wbzs',
      width: 100,
    },
    // 平均图像
    {
      title: '平均图像',
      key: 'pctx',
      dataIndex: 'pctx',
      width: 100,
    },
    // 无痛人数
    {
      title: '无痛人数',
      key: 'wtrs',
      dataIndex: 'wtrs',
      width: 100,
    },
    // 无痛率%
    {
      title: '无痛率%',
      key: 'wtl',
      dataIndex: 'wtl',
      width: 100,
    },
    // 染色人数
    {
      title: '染色人数',
      key: 'rsrs',
      dataIndex: 'rsrs',
      width: 100,
    },
    // 染色率%
    {
      title: '染色率%',
      key: 'rsl',
      dataIndex: 'rsl',
      width: 100,
    },
    // 活检人数
    {
      title: '活检人数',
      key: 'hjrs',
      dataIndex: 'hjrs',
      width: 100,
    },
    // 活检率%
    {
      title: '活检率%',
      key: 'hjl',
      dataIndex: 'hjl',
      width: 100,
    },
    // HP检查人数
    {
      title: 'HP检查人数',
      key: 'hpjcrs',
      dataIndex: 'hpjcrs',
      width: 120,
    },
    // HP检查率%
    {
      title: 'HP检查率%',
      key: 'hpjcl',
      dataIndex: 'hpjcl',
      width: 120,
    },
    // HP阳性人数
    {
      title: 'HP阳性人数',
      key: 'hpyxrs',
      dataIndex: 'hpyxrs',
      width: 120,
    },
  ];

  constructor(props) {
    super(props);
    this.state = {
      //城市集合
      cities: [],
      //区县集合
      districtAndStreet: [],
      //搜索条件
      searchCondition: {},
      //初始化数据
      data: [],
      // 被选中的行
      selectedRows: [],
      // 被选中后的行的id
      selectedRowKeys: [],
    };
  }

  componentDidMount() {
    this.getAllCities();
  }

  /**
   * 初始化数据
   * */
  getPageData = params => {
    this.execHttp('reportProgress/getPageData', {
      currentPage: 1,
      pageSize: 10,
      ...params,
    });
  };

  /**点击获取省下面的城市*/
  getAllCities = () => {
    this.execHttp('orgsetting/getAllProvinceCity', {
      parentRegionCode: '340000',
      user: this.props.currentUser.id,
    }).then(resp => {
      if (resp && resp.success) {
        const {
          success,
          data: { list },
        } = resp;
        if (success) {
          this.setState(
            {
              cities: list,
            },
            () => {
              // list.length为1表示为地市疾控账户登录
              if (list && list.length == 1) {
                this.searchform.setSelectedCity(list[0].id);
                // 加载区县数据
                this.getDistrictAndStreet(list[0].id);
              }
            }
          );
        }
      }
    });
  };

  /**获取城市下的区街道信息*/
  getDistrictAndStreet = val => {
    this.execHttp('orgsetting/getDistrictAndStreet', {
      parentRegionCode: val,
    }).then(resp => {
      if (resp && resp.success) {
        const { success, data } = resp;
        if (success) {
          this.setState({
            districtAndStreet: data.list,
          });
        }
      }
    });
  };

  render() {
    const { isLoading, data } = this.props;
    return (
      <div className={styles.netReportProgress}>
        <Card bordered={true} title={'网报进度'}>
          <SearchForm
            cityList={this.state.cities}
            initialDataTable={this.state.initialDataTable}
            districtAndStreetList={this.state.districtAndStreet}
            wrappedComponentRef={searchform => (this.searchform = searchform)}
            onSearchClick={this.handleOnSearchClick}
            onResetClick={this.handleOnResetClick}
            onExportClick={this.handleOnExportClick}
            OnSelectedCity={this.handleOnSelectedCity}
          ></SearchForm>
        </Card>

        <Card bordered={true} className={styles.contentBox}>
          <div>
            <StandardTable
              bordered
              loading={isLoading}
              data={data}
              hasSelection={false}
              selectedRows={this.state.selectedRows}
              columns={this.columns}
              onChange={this.handleTableChange}
              rowKey="id"
            />
          </div>
        </Card>
      </div>
    );
  }

  handleTableChange = (pagination, filters, sorter) => {
    const params = {
      currentPage: pagination.current,
      pageSize: pagination.pageSize,
    };
    // this.getPageData(params);
  };

  /**
   * 获取城市下的区街道信息
   * */
  handleOnSelectedCity = value => {
    if (value !== undefined) {
      this.getDistrictAndStreet(value);
    } else {
      this.setState({
        districtAndStreet: [],
      });
    }
  };

  /**
   * 导出按钮被点击
   * */
  handleOnExportClick = params => {
    // this.exportData(params);
  };

  /**
   * 搜索按钮点击
   * */
  handleOnSearchClick = params => {
    const type = params['type'];
    if (type) {
      this.setState({
        initialDataTable: type,
      });
    }
    // this.getPageData(params);
  };

  /**
   * 重置按钮点击
   * */
  handleOnResetClick = () => {
    // this.setState({
    //                 initialDataTable: 1,
    //               });
    // this.getPageData({});
  };
}

export default NetReportProgress;
